const { app, BrowserWindow, protocol } = require("electron");
// import { app, BrowserWindow, protocol } from "electron";
const path = require("path");

function createWindow() {
  // Create the browser window.
  const mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      preload: path.join(__dirname, "preload.ts"),
    },
  });

  if (process.env.npm_lifecycle_event === "electron:dev") {
    mainWindow.loadURL("http://localhost:3000");
    mainWindow.webContents.openDevTools();
  } else {
    mainWindow.loadFile("./dist/index.html");
  }
}
app.whenReady().then(() => {
  createWindow();

  // 能够加载本地的file
  protocol.registerFileProtocol("atom", (request, callback) => {
    const url = request.url.substr(7);
    callback(decodeURI(path.normalize(url)));
  });

  app.on("activate", function () {
    if (BrowserWindow.getAllWindows().length === 0) createWindow();
  });
});

app.on("window-all-closed", function () {
  if (process.platform !== "darwin") app.quit();
});
